package com.jkoss.wine.investment.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.investment.entity.InvestmentFollow;
import com.jkoss.wine.investment.service.IInvestmentFollowService;
import com.jkoss.wine.investment.service.IInvestmentService;
import com.jkoss.wine.investment.vo.InvestmentFollowVo;
import com.jkoss.wine.investment.vo.InvestmentVo;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.system.service.IMerchantEmployeesService;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 招商跟进记录 前端控制器
 * 
 * @Author Ning
 * @Version 1.0, 2018-10-26
 * @See
 * @Since com.jkoss.wine.investment.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/investment/investmentFollow")
public class InvestmentFollowController extends BaseController {

	@Autowired
	private IInvestmentFollowService iInvestmentFollowService;
	@Autowired
	private IInvestmentService iInvestmentService;
	@Autowired
	private IMerchantEmployeesService iMerchantEmployeesService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/investment/investmentFollow/list")
	public String list(DwzPageBean dwzPageBean, ModelMap map, String id, String state, String today, HttpServletRequest request, HttpServletResponse response) {
		map.put("id", id);
		Wrapper wrapper2 = Condition.create().eq("i.id", id);
		map.put("investments", iInvestmentService.selectVoByid(wrapper2));
		if (!CommonUtil.isBlank(state)) {
			dwzPageBean.getCountResultMap().put("state", state);
		}
		if (!CommonUtil.isBlank(today)) {
			dwzPageBean.getCountResultMap().put("today", today);
		}

		Wrapper wrapper = Condition.create().eq("ifw.imid", id).ne("ifw.del", 1);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("ifw.add_time", false);
		}
		Page resultPage = iInvestmentFollowService.selectVoPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "investment/investmentFollow/list";
	}

	/**
	 * 去新增
	 * 
	 * @param map
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/investment/investmentFollow/toInsert")
	public String toInsert(ModelMap map, String id, HttpServletRequest request, HttpServletResponse response) {
		map.put("id", id);
		Wrapper wrapper = Condition.create().ne("del", 1);
		List<MerchantEmployees> mEmployees = iMerchantEmployeesService.selectList(wrapper);
		map.put("mEmployees", mEmployees);
		return "investment/investmentFollow/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/investment/investmentFollow/toInsert")
	@ResponseBody
	public Object insert(@Valid InvestmentFollow investment_follow, HttpServletRequest request, HttpServletResponse response) {
		// 添加时间
		investment_follow.setAddTime(CommonUtil.getDate());
		// 操作人
		investment_follow.setOperator(getUserId());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		investment_follow.setDel(0);
		// 跟进人
		investment_follow.setMaid(getUserId());
		// 跟进时间
		// investment_follow.setFwtm(CommonUtil.date6());
		if (iInvestmentFollowService.insertInvestmentFollow(investment_follow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/investment/investmentFollow/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		List<MerchantEmployees> mEmployees = iMerchantEmployeesService.selectList(null);
		map.put("record", iInvestmentFollowService.selectById(id));
		map.put("mEmployees", mEmployees);
		return "investment/investmentFollow/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/investment/investmentFollow/toUpdate")
	@ResponseBody
	public Object update(@Valid InvestmentFollow investment_follow, HttpServletRequest request, HttpServletResponse response) {
		if (iInvestmentFollowService.updateById(investment_follow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/investment/investmentFollow/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList<>();
		for (String string : id) {
			InvestmentFollow investmentFollow = new InvestmentFollow();
			investmentFollow.setId(string);
			investmentFollow.setDel(1);
			list.add(investmentFollow);
		}
		if (iInvestmentFollowService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/investment/investmentFollow/export")
	public String export(ModelMap map, DwzPageBean dwzPageBean, String id, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().eq("ifw.imid", id).ne("ifw.del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("ifw.maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iInvestmentFollowService.selectVoPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "跟进记录", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, InvestmentFollowVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "跟进记录");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 去详情
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toDetails")
	@RequiresPermissions("/investment/investmentFollow/toDetails")
	public String toDetails(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		List<MerchantEmployees> mEmployees = iMerchantEmployeesService.selectList(null);
		map.put("record", iInvestmentFollowService.selectById(id));
		map.put("mEmployees", mEmployees);
		return "investment/investmentFollow/details";
	}

}
